package com.nl.controller.web.controller;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.model.Response;
import com.nl.controller.common.util.ResponseUtil;
import com.nl.controller.web.bean.SmsSendCompare;
import com.nl.controller.web.service.SmsSendCompareService;
import com.nl.controller.web.util.ExcelUtil;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("smsSendCompare")
@Slf4j
public class SmsSendCompareController {
    @Autowired
    private SmsSendCompareService smsSendCompareService;

    @ApiOperation(value = "同比环比查看", notes = "分页同比环比")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = true, dataType = "String"),
            @ApiImplicitParam(name = "flag", value = "查询方式", required = true, dataType = "String"),
            @ApiImplicitParam(name = "cityId", value = "地市编码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "countyId", value = "区县编码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "mgrId", value = "项目编码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "mgrName", value = "项目名称", required = true, dataType = "String")
    })
    @PostMapping("find")
    public Response findSmsSendCompare(
            @RequestParam(value = "page", required = true) int page,
            @RequestParam(value = "limit", required = true) int limit,
            @RequestParam(value = "flag", required = true) String flag,
            @RequestParam(value = "cityId", required = false) String cityId,
            @RequestParam(value = "countyId", required = false) String countyId,
            @RequestParam(value = "mgrId", required = false) String mgrId,
            @RequestParam(value = "mgrName", required = false) String mgrName,
            @RequestParam Map<String, Object> paramMap

    ) {
    	if ((cityId == null)&&(("mgr".equals(flag))||("county".equals(flag)))){
            throw new SmsException("请选择查询条件，开始查询");
        }else {
        PageInfo<SmsSendCompare> pageInfo = smsSendCompareService.findSmsSendCompare(page, limit, flag,cityId,countyId,mgrId,mgrName);
        log.info("同比环比查看：{}",paramMap.toString());
        log.debug("同比环比返回数据：{}", pageInfo.getList().toString());
        if(pageInfo.getList().size()==1){
        	 throw new SmsException("无数据");
        }else{
        return ResponseUtil.sucess(pageInfo.getList(), pageInfo.getTotal());
        }
        }
    }


    @ApiOperation(value = "导出", notes = "导出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "flag", value = "查询方式", required = true, dataType = "String"),
            @ApiImplicitParam(name = "cityId", value = "查询方式", required = true, dataType = "String"),
            @ApiImplicitParam(name = "countyId", value = "查询方式", required = true, dataType = "String"),
            @ApiImplicitParam(name = "mgrId", value = "查询方式", required = true, dataType = "String"),
            @ApiImplicitParam(name = "mgrName", value = "查询方式", required = true, dataType = "String")
    })
    @GetMapping("export")
    public void export(
            HttpServletRequest request, HttpServletResponse response,
            @RequestParam(value = "flag", required = true) String flag,
            @RequestParam(value = "cityId", required = false) String cityId,
            @RequestParam(value = "countyId", required = false) String countyId,
            @RequestParam(value = "mgrId", required = false) String mgrId,
            @RequestParam(value = "mgrName", required = false) String mgrName

    ) {
        JSONArray ja = smsSendCompareService.download(flag,cityId,countyId,mgrId,mgrName);//获取业务数据集
        Map<String, String> headMap = new LinkedHashMap<>();
        String title = "";
        if ("city".equals(flag)) {
            headMap.put("cityName", "地市");
            headMap.put("nowNumber", "上个月发送量");
            headMap.put("lastMonthNumber", "上上月发送量");
            headMap.put("lastYearMonthNumber", "去年上个月发送量");
            headMap.put("compareYearLastMonthNumber", "同比");
            headMap.put("compareLastMonthNumber", "环比");
            title = "短信统计表（按地市）";
        } else if ("mgr".equals(flag)) {
            title = "短信统计表（按项目）";
            headMap.put("cityName", "地市");
            headMap.put("countyName", "区县");
            headMap.put("mgrName", "项目名称");
            headMap.put("nowNumber", "上个月发送量");
            headMap.put("lastMonthNumber", "上上月发送量");
            headMap.put("lastYearMonthNumber", "去年上个月发送量");
            headMap.put("compareYearLastMonthNumber", "同比");
            headMap.put("compareLastMonthNumber", "环比");
        }
        else if ("county".equals(flag)) {
            title = "短信统计表（按项目）";
            headMap.put("cityName", "地市");
            headMap.put("countyName", "区县");
            headMap.put("nowNumber", "上个月发送量");
            headMap.put("lastMonthNumber", "上上月发送量");
            headMap.put("lastYearMonthNumber", "去年上个月发送量");
            headMap.put("compareYearLastMonthNumber", "同比");
            headMap.put("compareLastMonthNumber", "环比");
        }
        ExcelUtil.downloadExcelFile(title, headMap, ja, response,request);
        System.out.println("共" + ja.size() + "条数据,执行");
    }
}
